package com.example.lenovo.a203_yhy_ordering_3.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.beans.RegisterBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.RegisterListener;
import com.example.lenovo.a203_yhy_ordering_3.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity {

    private Button btn_reg;
    private Button btn_return;
    private EditText username;
    private EditText userpass;
    private EditText mobilphone;
    private EditText address;
    private EditText comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initViews();
        initEnents();
    }

    private void initViews() {
        btn_reg = (Button) findViewById(R.id.button3);
        btn_return = (Button) findViewById(R.id.button4);
        username = (EditText) findViewById(R.id.editText3);
        userpass = (EditText) findViewById(R.id.editText4);
        mobilphone = (EditText) findViewById(R.id.editText5);
        address = (EditText) findViewById(R.id.editText6);
        comment = (EditText) findViewById(R.id.editText7);
    }

    RegisterListener registerlistener = new RegisterListener() {
        @Override
        public void onResponse(RegisterBean register) {
            if(register.getSuccess().equals("0")) {
                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                final Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(RegisterActivity.this, "用户已注册", Toast.LENGTH_SHORT).show();
        }
    };


    private void initEnents(){
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }
                else if(userpass.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }
                else if(mobilphone.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入电话", Toast.LENGTH_SHORT).show();
                }
                else if(address.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入地址", Toast.LENGTH_SHORT).show();
                }
                else if(comment.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入备注", Toast.LENGTH_SHORT).show();
                }
                else{
                    RegisterModel registerModel = new RegisterModel();
                    registerModel.getResultList(username.getText().toString(),
                            userpass.getText().toString(),
                            mobilphone.getText().toString(),
                            address.getText().toString(),
                            comment.getText().toString(),
                            registerlistener);
                }
            }
        });
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

}
